package view;

import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.border.EtchedBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.text.DateFormatter;

import controller.CustomerTableModel;

import model.Zimmer;

/**
 * @author david
 */

public class CreateReservationView extends JPanel {
	private JTextField searchForCustomer;
	private JTable resultForCustomerSearch;
	private JComboBox searchForFields;
	private HashMap<String, String> searchByItems;
	private JTable selectedCustomers;
	private JSpinner toSpinner, fromSpinner;
	private JTable roomResultTable;
	private SpinnerDateModel toSpinnerModel, fromSpinnerModel;
	private JButton cancelButton, createReservationButton;

	public CreateReservationView() {
		initCreateReservationView();
	}

	public void initCreateReservationView(){
		this.setLayout(new BorderLayout());
		JPanel northSearchPanel = new JPanel();
		JPanel centerPanel = new JPanel();
		JPanel southPanel = new JPanel();
		southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.Y_AXIS));
		centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
		searchForCustomer = new JTextField("Search for new customer to add");
		searchForCustomer.setColumns(20);
		resultForCustomerSearch = new JTable();
		selectedCustomers = new JTable();
		searchForFields = new JComboBox();
		searchByItems = new HashMap<String, String>();

		//creating panel for finding right customer
		
		searchByItems.put("By Firstname", "vorname");
		searchByItems.put("By Lastname", "nachname");
		searchByItems.put("By Address", "rechnungsadresse");
		searchByItems.put("By Phone", "telefon");
		
		for( String key : searchByItems.keySet()){
			searchForFields.addItem(key);
		}
		
		northSearchPanel.add(new JLabel("Search for Customer"));
		northSearchPanel.add(searchForFields);
		northSearchPanel.add(searchForCustomer);
		this.add(northSearchPanel, BorderLayout.NORTH);
		//centerPanel.setBackground(Color.WHITE);
		
		JPanel table1Panel = new JPanel();
		JPanel table2Panel = new JPanel();
		
		table1Panel.setLayout(new BorderLayout());
		table1Panel.setBorder(BorderFactory.createEtchedBorder());
//		table1Panel.add(resultForCustomerSearch.getTableHeader(), BorderLayout.PAGE_START);
//		table1Panel.add(resultForCustomerSearch, BorderLayout.CENTER);
		JScrollPane resultCustomersPane = new JScrollPane(resultForCustomerSearch);
		table1Panel.add(resultCustomersPane);
		
		table2Panel.setLayout(new BorderLayout());
		table2Panel.setBorder(BorderFactory.createEtchedBorder());
//		table2Panel.add(selectedCustomers.getTableHeader(), BorderLayout.PAGE_START);
//		table2Panel.add(selectedCustomers, BorderLayout.CENTER);
		JScrollPane selectedCustomersPane = new JScrollPane(selectedCustomers);
		table2Panel.add(selectedCustomersPane);
		
		JPanel labelPanel = new JPanel();
		labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.X_AXIS));
		labelPanel.add(new JLabel("Select Customers for reservation"));
		labelPanel.add(Box.createHorizontalGlue());
		centerPanel.add(labelPanel);
		
		JPanel label2Panel = new JPanel();
		label2Panel.setLayout(new BoxLayout(label2Panel, BoxLayout.X_AXIS));
		label2Panel.add(new JLabel("Selected Customers"));
		label2Panel.add(Box.createHorizontalGlue());
		centerPanel.add(labelPanel);
		//centerPanel.add(Box.createHorizontalGlue());
		centerPanel.add(table1Panel);
		centerPanel.add(label2Panel);
		centerPanel.add(table2Panel);
		
		
		//date selection
		JPanel selectDatePanel = new JPanel();
		JPanel fromPanel = new JPanel();
		JPanel toPanel = new JPanel();
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR,0);
		calendar.set(Calendar.MINUTE, 0);
		Date initDate = calendar.getTime();
		calendar.add(Calendar.YEAR, -1);
		Date earliestDate = calendar.getTime();
		calendar.add(Calendar.YEAR, 2);
		Date latestDate = calendar.getTime();
		fromSpinnerModel = new SpinnerDateModel(initDate,
		                             earliestDate,
		                             latestDate,
		                             Calendar.DAY_OF_MONTH);
		fromSpinnerModel.setCalendarField(Calendar.DAY_OF_MONTH);
		fromSpinner = new JSpinner(fromSpinnerModel);
		fromSpinner.setEditor(new JSpinner.DateEditor(fromSpinner, "dd-MM-yyyy"));
		
		calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR,0);
		calendar.set(Calendar.MINUTE, 0);
		initDate = calendar.getTime();
		calendar.add(Calendar.YEAR, -1);
		earliestDate = calendar.getTime();
		calendar.add(Calendar.YEAR, 2);
		latestDate = calendar.getTime();
		toSpinnerModel = new SpinnerDateModel(initDate,
		                             earliestDate,
		                             latestDate,
		                             Calendar.DAY_OF_MONTH);
		toSpinner = new JSpinner(toSpinnerModel);
		toSpinner.setEditor(new JSpinner.DateEditor(toSpinner, "dd-MM-yyyy"));
		
		fromPanel.setLayout(new BoxLayout(fromPanel, BoxLayout.X_AXIS));
		fromPanel.setBorder(BorderFactory.createTitledBorder("From"));
		fromPanel.add(fromSpinner);
		fromPanel.add(Box.createHorizontalGlue());
		
		toPanel.setLayout(new BoxLayout(toPanel, BoxLayout.X_AXIS));
		toPanel.setBorder(BorderFactory.createTitledBorder("To"));
		toPanel.add(toSpinner);
		toPanel.add(Box.createHorizontalGlue());
		
		selectDatePanel.setLayout(new BoxLayout(selectDatePanel, BoxLayout.Y_AXIS));
		selectDatePanel.add(new JLabel("Select Date for reservation"));
		selectDatePanel.add(fromPanel);
		selectDatePanel.add(toPanel);
		selectDatePanel.add(toPanel);
		selectDatePanel.add(Box.createHorizontalGlue());
		
		//select room(s)

		roomResultTable = new JTable();
		JScrollPane roomScrollpane = new JScrollPane(roomResultTable);
		JPanel selectRoomsPanel = new JPanel();
		selectRoomsPanel.setLayout(new BoxLayout(selectRoomsPanel, BoxLayout.Y_AXIS));
		selectRoomsPanel.add(new JLabel("Please Mark Room(s) for reservation"));
		selectRoomsPanel.add(roomScrollpane);
		selectRoomsPanel.add(Box.createVerticalGlue());
		
		JPanel bottomMenu = new JPanel();
		cancelButton = new JButton("Cancel");
		createReservationButton = new JButton("Create Reservation");
		
		
		bottomMenu.setLayout(new BoxLayout(bottomMenu, BoxLayout.X_AXIS));
		bottomMenu.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		bottomMenu.add(Box.createHorizontalGlue());
		bottomMenu.add(cancelButton);
		bottomMenu.add(createReservationButton);
		
		
		
		southPanel.setLayout(new BorderLayout());
		southPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		southPanel.add(selectDatePanel, BorderLayout.NORTH);
		southPanel.add(selectRoomsPanel, BorderLayout.CENTER);
		//southPanel.add(bottomMenu, BorderLayout.SOUTH);
		centerPanel.add(Box.createVerticalStrut(5));
		centerPanel.add(southPanel);
		centerPanel.add(Box.createVerticalGlue());
		
		this.add(bottomMenu, BorderLayout.SOUTH);
		this.add(centerPanel, BorderLayout.CENTER);
		
		roomResultTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		selectedCustomers.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		resultForCustomerSearch.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		this.setVisible(true);
	}

	/*
	 * ############## Getters and setters will follow ###############
	 */
	public JTextField getSearchForCustomer() {
		return searchForCustomer;
	}

	public void setSearchForCustomer(JTextField searchForCustomer) {
		this.searchForCustomer = searchForCustomer;
	}

	public JTable getResultForCustomerSearch() {
		return resultForCustomerSearch;
	}

	public JComboBox getSearchForFields() {
		return searchForFields;
	}

	public void setSearchForFields(JComboBox searchForFields) {
		this.searchForFields = searchForFields;
	}

	public HashMap<String, String> getSearchByItems() {
		return searchByItems;
	}

	public void setSearchByItems(HashMap<String, String> searchByItems) {
		this.searchByItems = searchByItems;
	}

	public JTable getSelectedCustomers() {
		return selectedCustomers;
	}

	public void setSelectedCustomers(JTable selectedCustomers) {
		this.selectedCustomers = selectedCustomers;
	}

	public JSpinner getToSpinner() {
		return toSpinner;
	}

	public void setToSpinner(JSpinner toSpinner) {
		this.toSpinner = toSpinner;
	}

	public JSpinner getFromSpinner() {
		return fromSpinner;
	}

	public void setFromSpinner(JSpinner fromSpinner) {
		this.fromSpinner = fromSpinner;
	}

	public JTable getRoomResultTable() {
		return roomResultTable;
	}

	public void setRoomResultTable(JTable roomResultTable) {
		this.roomResultTable = roomResultTable;
	}

	public SpinnerDateModel getToSpinnerModel() {
		return toSpinnerModel;
	}

	public void setToSpinnerModel(SpinnerDateModel toSpinnerModel) {
		this.toSpinnerModel = toSpinnerModel;
	}

	public SpinnerDateModel getFromSpinnerModel() {
		return fromSpinnerModel;
	}

	public void setFromSpinnerModel(SpinnerDateModel fromSpinnerModel) {
		this.fromSpinnerModel = fromSpinnerModel;
	}

	public JButton getCancelButton() {
		return cancelButton;
	}

	public void setCancelButton(JButton cancelButton) {
		this.cancelButton = cancelButton;
	}

	public JButton getCreateReservationButton() {
		return createReservationButton;
	}

	public void setCreateReservationButton(JButton createReservation) {
		this.createReservationButton = createReservation;
	}
	
	
	
}


